package com.interview.item.mybatisplus.mybatisplusdemo.creatdata;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @Author:zxh
 * @Description:
 * @Date: 2020/4/27
 * @Modified By:
 */
public class JDBCdemo {
    private static Logger logger = LoggerFactory.getLogger(JDBCdemo.class);

    static final String driver = "com.mysql.jdbc.Driver";
    static final String url = "jdbc:mysql://localhost:3306/interview_item";

    //数据库的用户名与密码
    static final String user = "root";
    static final String password = "root123";

    public static void main(String[] args) {

        Connection connection = null;
        Statement statement = null;
        try {
            //注册驱动
            Class.forName(driver);

            //打开连接
            logger.info("连接数据库....");
            connection = DriverManager.getConnection(url, user, password);

            //执行查询
            logger.info("实例化statement对象.....");
            statement = connection.createStatement();
            String sql;
            sql = "SELECT id, name, url FROM websites";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                //通过字段检索
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String url = resultSet.getString("url");

                //输出数据
                logger.info("id{},name:{},url:{}", id, name, url);
            }
            resultSet.close();
            statement.close();
            connection.close();




        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
    }
}
